define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();
            
            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });
            
            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            first: function () {
                Table.api.init({
                    /*extend: {
                        index_url: 'supplier/index',
                        add_url: '',
                        edit_url: 'supplier/edit',
                        del_url: 'supplier/del',
                        multi_url: '',
                        table: '',
                    }*/
                },
                {
                    custom:{'审核通过':'success','审核未通过':'danger','审核中':'warning'}
                }
                );
                // 表格1
                var table1 = $("#site");
                table1.bootstrapTable({
                    url: 'supplier/site',
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: 'supplier/edit',
                        del_url: 'supplier/del',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'id', title: 'ID', operate:false},
                            {field: 'avatar', title: '头像', operate:false, formatter: Controller.api.formatter.thumb},
                            {field: 'mobile', title: __('Mobile'), operate: 'LIKE %...%'},
                            {field: 'name', title: '场地商名称', operate: 'LIKE %...%'},
                            {field: 'person', title: '负责人姓名', operate: 'LIKE %...%'},
                            {field: 'city', title: '所在城市', operate: 'LIKE %...%'},
                           /* {field: 'address', title: '具体地址', operate: 'LIKE %...%'},*/
                            {field: 'sum_income', title: __('Sum_income'), operate:false},
                            {field: 'wallet_money', title: __('Wallet_money'), operate:false},
                            {field: 'cash', title: __('Cash'), operate:false},
                            {field: 'state_text', title: __('State'), operate:false,formatter: Table.api.formatter.label},
                            {field: 'operate', title: __('Operate'), table: table1, 
                                buttons:[{
                                    name: 'aclist', 
                                    text: '发布场地列表', 
                                    classname: 'btn btn-xs btn-info btn-fields btn-addtabs', 
                                    icon: 'fa fa-list', 
                                    url: 'activity/index?supplier_id={ids}'
                                },
                                {
                                    name: 'detail', 
                                    text: '详情', 
                                    classname: 'btn btn-xs btn-info btn-detail btn-dialog', 
                                    icon: 'fa fa-list', 
                                    url: 'supplier/detail'
                                }],
                                events: Table.api.events.operate, formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#activity");
                table2.bootstrapTable({
                    url: 'supplier/activity',
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: 'supplier/edit',
                        del_url: 'supplier/del',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'id', title: 'ID', operate:false},
                            {field: 'avatar', title: '头像', operate:false, formatter: Controller.api.formatter.thumb},
                            {field: 'mobile', title: __('Mobile'), operate: 'LIKE %...%'},
                            {field: 'name', title: '公司名称', operate: 'LIKE %...%'},
                            {field: 'person', title: '负责人姓名', operate: 'LIKE %...%'},
                            {field: 'city', title: '所在城市', operate: 'LIKE %...%'},
                            {field: 'address', title: '具体地址', operate: 'LIKE %...%'},
                            {field: 'is_ticket', title: '是否开票'},
                            {field: 'sum_income', title: __('Sum_income'), operate:false},
                            {field: 'wallet_money', title: __('Wallet_money'), operate:false},
                            {field: 'cash', title: __('Cash'), operate:false},
                            {field: 'state_text', title: __('State'), operate:false,formatter: Table.api.formatter.label},
                            {field: 'operate', title: __('Operate'), table: table2, 
                                buttons:[{
                                    name: 'aclist', 
                                    text: '发布活动列表', 
                                    classname: 'btn btn-xs btn-info btn-fields btn-addtabs', 
                                    icon: 'fa fa-list', 
                                    url: 'activity/index?supplier_id={ids}'
                                },
                                {
                                    name: 'detail', 
                                    text: '详情', 
                                    classname: 'btn btn-xs btn-info btn-detail btn-dialog', 
                                    icon: 'fa fa-list', 
                                    url: 'supplier/detail'
                                }],
                                events: Table.api.events.operate, formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            },

            third: function () {
                // 表格2
                var table3 = $("#master");
                table3.bootstrapTable({
                    url: 'supplier/master',
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: 'supplier/edit',
                        del_url: 'supplier/del',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar3',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
                            {field: 'id', title: 'ID', operate:false},
                            {field: 'avatar', title: '头像', operate:false, formatter: Controller.api.formatter.thumb},
                            {field: 'mobile', title: __('Mobile'), operate: 'LIKE %...%'},
                            {field: 'name', title: '主讲老师', operate: 'LIKE %...%'},
                            {field: 'truename', title: '真实姓名', operate: 'LIKE %...%'},
                            {field: 'city', title: '所在城市', operate: 'LIKE %...%'},
                            {field: 'office',title:'任职机构', operate: 'LIKE %...%'},
                            {field: 'company_post',title:'担任职务', operate: 'LIKE %...%'},
                            {field: 'sum_income', title: __('Sum_income'), operate:false},
                            {field: 'wallet_money', title: __('Wallet_money'), operate:false},
                            {field: 'cash', title: __('Cash'), operate:false},
                            {field: 'state_text', title: __('State'), operate:false,formatter: Table.api.formatter.label},
                            {field: 'operate', title: __('Operate'), table: table3, 
                                buttons:[{
                                    name: 'aclist', 
                                    text: '发布课程列表', 
                                    classname: 'btn btn-xs btn-info btn-fields btn-addtabs', 
                                    icon: 'fa fa-list', 
                                    url: 'activity/index?supplier_id={ids}'
                                },
                                {
                                    name: 'detail', 
                                    text: '详情', 
                                    classname: 'btn btn-xs btn-info btn-detail btn-dialog', 
                                    icon: 'fa fa-list', 
                                    url: 'supplier/detail'
                                }],
                                events: Table.api.events.operate, formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table3);
            }
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                thumb: function (value, row, index) {
                        return '<a href="' + row.avatar + '" target="_blank"><img src="' +  value  + '" alt="" style="max-height:50px;max-width:60px"></a>';
                },
                url: function (value, row, index) {
                    return '<a href="' + row.avatar + '" target="_blank" class="label bg-green">' + value + '</a>';
                },
            },
        }
    };
    return Controller;
});